﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Castle.Core.Logging;

namespace TVN.ShopMan.ApplicationInterface.AppForms
{
    using TVN.Common.Interfaces;
    using TVN.ShopMan.ActiveModels;
    using TVN.ShopMan.ApplicationInterface.Helpers;

    public partial class ProductEditForm : Form
    {
        private static readonly ILogger logger = Program.Logger.CreateChildLogger("ProductFormEdit");
        private IRepository<Product> accountRepository = null;
        private IRepository<Unit> unitRepository = null;
        private IRepository<Supplier> supplierRepository = null;
        private Product selectedProduct = null;

        #region Properties

        public Product SelectedProduct
        {
            get { return this.selectedProduct; }
            set { this.selectedProduct = value; }
        }

        #endregion

        #region Constructors

        public ProductEditForm()
        {
            InitializeComponent();

            this.SetUp();
            this.LoadProductData();
        }

        public ProductEditForm(Product accountItem)
        {
            InitializeComponent();

            this.selectedProduct = accountItem;
            this.SetUp();
            this.LoadProductData();
        }

        #endregion

        #region Private methods

        private void SetUp()
        {
            this.accountRepository = Program.Container.Resolve<IRepository<Product>>();
            this.unitRepository = Program.Container.Resolve<IRepository<Unit>>();
            this.supplierRepository = Program.Container.Resolve<IRepository<Supplier>>();
        }

        private void LoadProductData()
        {
            IList<Unit> units = this.unitRepository.FindAll();
            units.Insert(0, new Unit
            {
                Name = "---"
            });
            this.cboUnits.DataSource = units;
            this.cboUnits.ValueMember = Unit.Properties.Id;
            this.cboUnits.DisplayMember = Unit.Properties.Name;

            IList<Supplier> suppliers = this.supplierRepository.FindAll();
            suppliers.Insert(0, new Supplier
            {
                Name = "---"
            });
            this.cboSupplier.DataSource = suppliers;
            this.cboSupplier.ValueMember = Supplier.Properties.Id;
            this.cboSupplier.DisplayMember = Supplier.Properties.Name;

            if (this.selectedProduct != null)
            {
                this.txtName.Text = this.selectedProduct.Name;
                this.txtDescription.Text = this.selectedProduct.Description;

                this.cboUnits.SelectedValue = this.selectedProduct.Unit.Id;
                this.cboSupplier.SelectedValue = this.selectedProduct.Supplier.Id;
                this.nudPrice.Value = this.selectedProduct.Price;
            }
        }

        /// <summary>
        /// Assign current information into Account object
        /// </summary>
        /// <returns></returns>
        private void AssignProductData()
        {
            try
            {
                if (this.selectedProduct == null)
                {
                    this.selectedProduct = new Product
                    {
                        CreatedDate = DateTime.Now,
                        Creator = Program.SessionData.ActiveAccount,
                        Cost = 0
                    };
                }
                else
                {
                    this.selectedProduct.LastModifiedDate = DateTime.Now;
                    this.selectedProduct.LastModifier = Program.SessionData.ActiveAccount;
                }

                this.selectedProduct.Name = this.txtName.Text;
                this.selectedProduct.Description = this.txtDescription.Text;
                this.selectedProduct.Price = this.nudPrice.Value;
                this.selectedProduct.Unit = (Unit)this.cboUnits.SelectedItem;
                this.selectedProduct.Supplier = (Supplier)this.cboSupplier.SelectedItem;
            }
            catch (Exception ex)
            {
                logger.Warn("Could not save account", ex);
            }
        }

        #endregion

        #region Events

        private void btnOK_Click(object sender, EventArgs e)
        {
            bool isCreating = ((this.selectedProduct == null) || (this.selectedProduct.Id <= 0));
            if (AppHelper<Product>.IsExists(Product.Properties.Name, this.txtName.Text, isCreating))
            {
                this.txtName.Focus();
            }
            else
            {
                this.AssignProductData();
                this.DialogResult = DialogResult.OK;
            }
        }

        #endregion
    }
}
